/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Principal;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;

/**
 *
 * @author pato
 */
public class editarEspeciales extends javax.swing.JDialog {

    /**
     * Creates new form transferirEspeciales
     */
    PreparedStatement ps;
    ResultSet rs;
    String tipo;
    int codigo;
    private final DefaultListModel modelo1;
    private final DefaultListModel modelo2;
    Vector vendiendo,vendiendoId;
    PrecioCantidad prcd;
    public editarEspeciales(java.awt.Frame parent, boolean modal, String t, int c, Vector vendiendo,Vector vendiendoId,PrecioCantidad prcd) {
        super(parent, modal);
        initComponents();
        modelo1 = new DefaultListModel();
        listaSuc1.setModel(modelo1);
        modelo2 = new DefaultListModel();
        listaSuc2.setModel(modelo2);
        this.vendiendo=vendiendo;
        this.vendiendoId=vendiendoId;
        codigo = c;
        tipo = t;
        this.prcd=prcd;
        cargarDatos();
    }

    private void cargarDatos() {
        modelo1.clear();
        modelo2.clear();
        String modelo= "";
        try {
            ps=local_connection.getconnect().prepareStatement("select nombre from productos where id_producto = ?");
            ps.setInt(1,codigo);
            rs=ps.executeQuery();
            if(rs.next())
                super.setTitle(codigo+"-"+rs.getString(1));
          
            String idprod = (tipo == "Chip") ? "" : " and id_producto=" + codigo;
            ps = local_connection.getconnect().prepareStatement("select clave from especiales where vendido=0 and tipo=?" + idprod + " and (sucursal = ?)");
            ps.setString(1, tipo);
            ps.setInt(2, VentanaPrincipal.sucursal);
            rs = ps.executeQuery();
            while (rs.next()) {
                modelo=rs.getString("clave");
                    if(vendiendo.contains(modelo))
                        modelo2.addElement(modelo);
                    else
                    modelo1.addElement(modelo);

            }
        } catch (SQLException ex) {
            Logger.getLogger(editarEspeciales.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        listaSuc1 = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        listaSuc2 = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jScrollPane1.setViewportView(listaSuc1);

        jScrollPane2.setViewportView(listaSuc2);

        jButton1.setText(">>");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("<<");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Aceptar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Cancelar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel1.setText("Disponibles");

        jLabel2.setText("Vendiendo");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4)
                .addGap(18, 18, 18))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(71, 71, 71)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addGap(97, 97, 97))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jButton1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton2))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton4)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        this.dispose();
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (!listaSuc1.isSelectionEmpty()) {
            String clave;
            List<String> claves = listaSuc1.getSelectedValuesList();
            for (int i = 0; i < claves.size(); i++) {
                clave = claves.get(i);
                modelo1.removeElement(clave);
                modelo2.addElement(clave);
            }
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (!listaSuc2.isSelectionEmpty()) {
            String clave;
            List<String> claves = listaSuc2.getSelectedValuesList();
            for (int i = 0; i < claves.size(); i++) {
                clave = claves.get(i);
                modelo2.removeElement(clave);
                modelo1.addElement(clave);
            }
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
       
        int i;
        if (!modelo1.isEmpty()) {
                for ( i = 0; i < modelo1.size(); i++) {
                    if(vendiendo.contains(modelo1.getElementAt(i))){
                        vendiendoId.removeElementAt(vendiendo.indexOf(modelo1.getElementAt(i)));
                        vendiendo.remove(modelo1.getElementAt(i));
                    }
                }   
            }
            
            if (!modelo2.isEmpty()) {
                for ( i = 0; i < modelo2.size(); i++) {
                    if(!vendiendo.contains(modelo2.getElementAt(i))){
                        vendiendo.addElement(modelo2.getElementAt(i));
                        vendiendoId.addElement(codigo);
                    }
                }   
            }
           prcd.setCantidad(modelo2.size());
        this.dispose();

    }//GEN-LAST:event_jButton3ActionPerformed

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList listaSuc1;
    private javax.swing.JList listaSuc2;
    // End of variables declaration//GEN-END:variables
}
